home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / TStr255 class / •AZN_TSTR255 / AZN_TStr255.cp next >
Encoding:
Text File  |  1995-10-23  |  3.9 KB  |  189 lines  |  [TEXT/CWIE]

  1. /*        
  2. *    AZN_TStr255.cp
  3. *
  4. *    Wrapper class for Pascal Str255
  5. *
  6. *    © Andrew Nemeth  Warrimoo Australia  1995
  7. *    aznemeng@zeta.org.au
  8. *
  9. *    File created:    5 Jun 95
  10. *    Modified:        5, 28, 29 Jun;
  11. *                2, 23 Oct 95.
  12. */
  13.  
  14. /*
  15. Some notes about this class
  16. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  17. It is meant to be a 'drop-in' replacement for the pascal Str255 string type.
  18. The idea being, where ever you would have used a Str255, you can now use this
  19. 'TStr255' class, with its overloaded operators to make your life easier.
  20.  
  21. Thus to create & initialise string(s):
  22.  
  23.     TStr255    myString;
  24.     TStr255    myOtherString = "\pInit. with me!";
  25.     TStr255    myLastString = myOtherString;
  26.     char        txtMine[ 32 ] = { "\0" };
  27.  
  28. To 'copy' string(s) (assignment):
  29.  
  30.     myString = "\pNew contents!";
  31.     myOtherString = myString;
  32. //                                                        note, can assign C Strings!
  33.     myLastString = "Can even copy C Strings!";
  34. //                                                        can assign ONTO C Strings!
  35.     myString->copy2C( txtMine );
  36.  
  37. To concat string(s):
  38.  
  39.     myString += "\p add me to end!";
  40.     myOtherString += myString;
  41.     myLastString += " can even concat C Strings!";
  42.  
  43.     myString->cat2C( txtMine );
  44.  
  45. Because of the overloaded 'StringPtr' operator, can pass objects of this class to
  46. functions which expect 'StringPtr' parameters!
  47.  
  48.     myString = "\p12";
  49. //                                                        toolbox call
  50.     ::StringToNum( myString, &lgNumber );
  51.     ::EqualString( "\p12", myString, TRUE, TRUE );
  52.  
  53. Finally, can access elements of the string array with the
  54. supplied [] operator, as well as grabbing the size of the
  55. 'string' by use of the * operator (just as you would for normal
  56. Str255's!)
  57.  
  58.     unsigned char        chElement = myString[2];
  59.     short            shSize = *myString;
  60.  
  61.  
  62. PROBLEMS YOU SHOULD BE AWARE OF:
  63.  
  64.     o    Typecasting to StringHandles etc. is going die;
  65.     o    Expect 'p2cStr' and the like to fail;
  66.     o    ptr manipulation is going to fail
  67.         etc. myString += 10; chElement = *myString; <- this will always return size!
  68. */
  69.  
  70.  
  71. #include    "AZN_TStr255.h"                                //    Wrapper class for Str255
  72.  
  73.  
  74.  
  75.  
  76. TStr255 &      TStr255::operator=( ConstStr255Param str255S )
  77. //
  78. //    Assign Str255
  79. //
  80. {
  81.     if ( ::EqualString( str255S, f_str255Item, TRUE, TRUE ) )
  82.         return( *this );
  83.  
  84.     ::BlockMoveData( StringPtr(str255S), f_str255Item, str255S[0] + 1 );
  85.  
  86.     return( *this );
  87. }
  88.  
  89.  
  90.  
  91. TStr255 &      TStr255::operator+=( ConstStr255Param str255S )
  92. //
  93. //    Concat Str255 onto class string,
  94. //    truncating if new str > 255
  95. //
  96. {
  97.     const short    kshCpyChars = minNum( str255S[0], 255 - f_str255Item[0] );
  98.  
  99.  
  100.     ::BlockMoveData( StringPtr(str255S) + 1, f_str255Item + (f_str255Item[0] + 1), long( kshCpyChars ) );
  101.     f_str255Item[0] += kshCpyChars;
  102.  
  103.     return( *this );
  104. }
  105.  
  106.  
  107.  
  108. TStr255 &      TStr255::operator=( const char * txtC )
  109. //
  110. //    Assign C string
  111. //    truncating if str > 255
  112. //
  113. {
  114.     const short    kshCpyChars = minNum( short( myStrlen( (char *)txtC ) ), 255 );
  115.  
  116.  
  117.     ::BlockMoveData( Ptr(txtC), f_str255Item + 1, long( kshCpyChars ) );
  118.     f_str255Item[0] = kshCpyChars;
  119.  
  120.     return( *this );
  121. }
  122.  
  123.  
  124.  
  125. TStr255 &      TStr255::operator+=( const char * txtC )
  126. //
  127. //    Concat C string onto class string,
  128. //    truncating if new str > 255
  129. //
  130. {
  131.     const short    kshCpyChars = minNum( short( myStrlen( (char *)txtC ) ), 255 - f_str255Item[0] );
  132.  
  133.  
  134.     ::BlockMoveData( Ptr(txtC), f_str255Item + (f_str255Item[0] + 1), long( kshCpyChars ) );
  135.     f_str255Item[0] += kshCpyChars;
  136.  
  137.     return( *this );
  138. }
  139.  
  140.  
  141.  
  142. void            TStr255::copy2C( char * txtC )
  143. //
  144. //    Given a C string, COPY the 
  145. //    class var 'Str255' onto it.
  146. //
  147. {
  148.     ::BlockMoveData( f_str255Item + 1, txtC, long( f_str255Item[0] ) );
  149.  
  150.     txtC += f_str255Item[0];
  151.     *txtC = '\0';
  152. }
  153.  
  154.  
  155.  
  156. void            TStr255::cat2C( char * txtC )        
  157. //
  158. //    Given a C string, CONCAT the 
  159. //    class var 'Str255' onto it.
  160. //
  161. {
  162.     const long        klgCLength = myStrlen( txtC );
  163.  
  164.  
  165.     ::BlockMoveData( f_str255Item + 1, txtC + klgCLength, long( f_str255Item[0] ) );
  166.  
  167.     txtC += ( klgCLength + f_str255Item[0] );
  168.     *txtC = '\0';
  169. }
  170.  
  171.  
  172.  
  173. long            TStr255::myStrlen( register char * txtItem )
  174. //
  175. //    Replacement for ANSI '::strlen()'
  176. //    (Hence no need to include ANSI libs in project)
  177. //
  178. {
  179.     register    long        lgLength = 0L;
  180.  
  181.     if ( txtItem )
  182.         {
  183.         while ( *txtItem++ )
  184.             lgLength++;
  185.         }
  186.  
  187.     return ( lgLength );
  188. }
  189.